package org.jeecg.modules.wlhy.hyvehicle.service.impl;

import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyDriverVehicle;
import org.jeecg.modules.wlhy.hyvehicle.mapper.HyDriverVehicleMapper;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyDriverVehicleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 司机车辆关系表
 * @Author: duanlikao
 * @Date:   2020-12-28
 * @Version: V1.0
 */
@Service
public class HyDriverVehicleServiceImpl extends ServiceImpl<HyDriverVehicleMapper, HyDriverVehicle> implements IHyDriverVehicleService {
    @Resource
    private  HyDriverVehicleMapper hyDriverVehicleMapper;
    @Override
    public HyDriverVehicle getInfoByDriverIdAndVehicleId(String driverId, String vehicleId) {
        return hyDriverVehicleMapper.getInfoByDriverIdAndVehicleId(driverId ,vehicleId);
    }
//根据车Id查询车辆
    @Override
    public List<HyDriverVehicle> getInfoByVehicleId (String vehicleId){
        QueryWrapper <HyDriverVehicle> queryWrapper = new QueryWrapper();
        queryWrapper.eq("vehicle_id", vehicleId);
        queryWrapper.eq("iz_used", 1);
        queryWrapper.orderByDesc("create_time");
        List<HyDriverVehicle> hyDriverVehicles = list(queryWrapper);
        return hyDriverVehicles ;
    }

    /**
     * 查询司机已绑定的车辆
     * @param driverId
     * @return
     */
    @Override
    public List <String> getVehicleIdListByDriverId(String driverId) {
        if(StringUtils.isEmpty(driverId)){
           return new ArrayList <>();
        }
        QueryWrapper <HyDriverVehicle> queryWrapper = new QueryWrapper();
        queryWrapper.eq("driver_id", driverId);
        queryWrapper.eq("iz_used", 1);
        List<HyDriverVehicle> listDriverVehicle = this.list(queryWrapper);

        return  CollectionUtils.isNotEmpty(listDriverVehicle)  ?
                listDriverVehicle.stream().map(HyDriverVehicle::getVehicleId).collect(Collectors.toList())
                : new ArrayList <>();
    }


    /**
     * 根据车辆id查询对应司机id
     */
    @Override
    public List<String> getDriverIdListByVehicleId(String vehicleId) {
        if(StringUtils.isEmpty(vehicleId)){
            return new ArrayList <>();
        }
        QueryWrapper <HyDriverVehicle> queryWrapper = new QueryWrapper();
        queryWrapper.eq("vehicle_id", vehicleId);
        queryWrapper.eq("iz_used", 1);
        List<HyDriverVehicle> listDriverVehicle = this.list(queryWrapper);

        return  CollectionUtils.isNotEmpty(listDriverVehicle)  ?
                listDriverVehicle.stream().map(HyDriverVehicle::getDriverId).collect(Collectors.toList())
                : new ArrayList <>();
    }

    /**
     * 更新司机车辆信息表；
     * @param vehicleId
     * @param izUsed
     * @return
     */
    @Override
    public Boolean updateByVehicleId(String vehicleId,String izUsed ){
        return hyDriverVehicleMapper.updateByVehicleId(vehicleId ,izUsed);
    }
}
